package com.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

    @RequestMapping("/login")
    public String dispatcher(){
        System.out.println("启动成功");
        return "/index.jsp";
    }

    @RequestMapping("/login2")
    public ModelAndView dispatcher2(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user", "张三");
        modelAndView.setViewName("/index.jsp");
        return modelAndView;
    }

    //回写数据
    @RequestMapping("/login3")
    @ResponseBody
    public String dispatcher3(){
        return "success";
    }

    //回写json串
    @RequestMapping("/login4")
    @ResponseBody
    public String dispatcher4() throws JsonProcessingException {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        ObjectMapper objectMapper = new ObjectMapper();
        final String writeValueAsString = objectMapper.writeValueAsString(user);
        return writeValueAsString;
    }

    //使用springMVC回写json串
    @RequestMapping("/login5")
    @ResponseBody
    public User dispatcher5() throws JsonProcessingException {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        return user;
    }

    //使用springMVC注解回写json串
    @RequestMapping("/login6")
    @ResponseBody
    public User dispatcher6() throws JsonProcessingException {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        return user;
    }
}
